<?php 

if (! defined('BASEPATH')) exit('No direct script access');

class movie_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_by_id($movie_id = FALSE)
    {
        if ($movie_id === FALSE)
        {
            return FALSE;
        }
        
        $query = $this->db->where('id', $movie_id)
                          ->get('movie');
        if ($query->num_rows() == 1)
        {
            return $query->row();
        }
        else
        {
            return FALSE;
        }
    }
    
    function get_all($num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('*,(SELECT name FROM sound WHERE id = movie.sound_id) sound_name' )
                          ->get('movie', $num, $offset);
        return $query->result();
    }
    
    function get_by_branch_id($branch_id,$num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('movie.*,(SELECT name FROM sound WHERE id = movie.sound_id) sound_name' )
                          ->join('cinema', 'cinema.id = schedule.cinema_id')
                          ->join('movie', 'movie.id = schedule.movie_id')
                          ->where('cinema.branch_id', $branch_id)
                          ->where('schedule.start_time > NOW() ')
                          ->group_by(array("movie.id", "movie.sound_id"))
                          ->get('schedule', $num, $offset);
        return $query->result();
    }
    
    
    function create($title = FALSE, $description = FALSE, $duration = FALSE , $sound_id = FALSE)
    {
        if ($title === FALSE OR $description === FALSE OR $duration === FALSE OR $sound_id === FALSE )
        {
            return FALSE;
        }

        $movie = array('title' => $title,
                         'description' => $description,
                         'duration' => $duration,
                         'sound_id' => $sound_id
                        );
        $this->db->insert('movie', $movie);
	    return $this->db->insert_id();
        
    }
    
    function update($movie_id = FAlSE , $title = FALSE, $description = FALSE, $duration = FALSE , $sound_id = FALSE)
    {
        if ($movie_id === FALSE OR $title === FALSE OR $description === FALSE OR $duration === FALSE OR $sound_id === FALSE )
        {
            return FALSE;
        }
        $movie = array('title' => $title,
                         'description' => $description,
                         'duration' => $duration,
                         'sound_id' => $sound_id
                        );
        $this->db->where('id', $movie_id);
        $this->db->update('movie', $movie);
        return TRUE;
    }
    
    function delete($movie_id = FALSE)
    {
        if ($movie_id === FALSE)
        {
            return FALSE;
        }
        $this->db->where('id', $movie_id);
        $this->db->delete('movie');
        return TRUE;
    }
    
   
}
/* End of file fixture_model.php */
/* Location: ./system/application/models/fixture_model.php */